package cn.tedu.micro.demo.all.service;

import cn.tedu.micro.demo.all.dao.mapper.CartMapper;
import cn.tedu.micro.demo.all.dao.mapper.OrderMapper;
import cn.tedu.micro.demo.all.dao.repository.CartRepository;
import cn.tedu.micro.demo.all.dao.repository.OrderRepositoryImpl;
import cn.tedu.micro.demo.all.dao.repository.StockRepository;
import cn.tedu.micro.demo.pojo.po.OrderPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderRepositoryImpl orderRepository;
    @Autowired
    private CartRepository cartRepository;
    @Autowired
    private StockRepository stockRepository;
    @Override
    public int addOrder(String userId, String productCode, Integer count) {
        OrderPO orderPO = new OrderPO();
        orderPO.setUserId(userId);
        orderPO.setProductCode(productCode);
        orderPO.setCount(count);
        int num=orderRepository.insert(orderPO);
        if(num>0){
            cartRepository.deleteByUserIdAndProductCode(userId,productCode);
            stockRepository.reduceCount(productCode,count);
        }

        return num;
    }
}
